package com.manhanlou.view;

import com.manhanlou.domain.*;
import com.manhanlou.service.*;
import com.manhanlou.utils.Utility;
import org.apache.commons.dbutils.DbUtils;

import java.util.Date;
import java.util.List;

/**
 * 主界面
 *
 * @author wty
 * @date 2022/11/9 0:43
 */
@SuppressWarnings({"all"})
public class MHLView {
    public static void main(String[] args) {
        MyView myView = new MyView();
        myView.showFirstMenu();
    }
}

class MyView {
    /**
     * 一级菜单的循环控制变量
     */
    private boolean firstMenuloop = true;
    /**
     * 一级菜单接收用户的输入
     */
    private String key;

    /**
     * 操作employee表
     */
    private EmployeeService employeeService = new EmployeeService();
    private DiningTableService diningTableService = new DiningTableService();
    private MenuService menuService = new MenuService();
    private BillService billService = new BillService();
    private MultiTableService multiTableService = new MultiTableService();
    private EmployeeInfoService employeeInfoService = new EmployeeInfoService();

    public void showFirstMenu() {
        while (firstMenuloop) {
            System.out.println("===========满汉楼==========");
            System.out.println("1.登录满汉楼");
            System.out.println("2.退出满汉楼");
            System.out.println("请输入你的选择:");
            key = Utility.readString(1);

            if ("1".equals(key)) {
                /**
                 * 二级菜单的循环控制变量
                 */
                System.out.println("请输入员工号");
                String empId = Utility.readString(50);
                System.out.println("请输入密码");
                String pwd = Utility.readString(50);

                if (isVarified(empId, pwd)) {
                    boolean secondMenuloop = true;
                    while (secondMenuloop) {
                        System.out.println("===========满汉楼二级菜单==========");
                        System.out.println("1.显示餐桌状态");
                        System.out.println("2.预定餐桌");
                        System.out.println("3.显示所有菜品");
                        System.out.println("4.点餐服务");
                        System.out.println("5.查看账单");
                        System.out.println("6.结账");
                        System.out.println("7.多表联合查询");
                        System.out.println("8.人事管理");
                        System.out.println("9.退出满汉楼");
                        System.out.println("请输入你的选择:");
                        key = Utility.readString(1);

                        switch (key) {
                            case "1":
                                System.out.println("====1.显示餐桌状态====");
                                diningTableService.queryState();
                                System.out.println("====显示完毕====");
                                break;
                            case "2":
                                System.out.println("====2.预定餐桌====");
                                orderTable();
                                break;
                            case "3":
                                System.out.println("====3.显示所有菜品====");
                                showMenu();
                                break;
                            case "4":
                                System.out.println("====4.点餐服务====");
                                orderMeals();
                                break;
                            case "5":
                                System.out.println("====5.查看账单====");
                                showBill();
                                break;
                            case "6":
                                System.out.println("====6.结账服务====");
                                payAccount();
                                break;
                            case "7":
                                System.out.println("====7.多表联合查询====");
                                multiQuery();
                                break;
                            case "8":
                                System.out.println("====7.人事管理====");
                                personMange();
                                break;
                            case "9":
                                secondMenuloop = false;
                                firstMenuloop = false;
                                break;
                            default:
                                System.out.println("输入有误,请重新输入");
                        }
                    } // while

                } else {
                    System.out.println("---登录失败---");
                }

            } else if ("2".equals(key)) {
                firstMenuloop = false;
                System.out.println("退出满汉楼系统!");
            } else {
                System.out.println("输入有误，请重新输入");
            }
        }
    }

    /**
     * 验证登录名和密码
     *
     * @param
     * @return boolean
     * @param: empId
     * @param: pwd
     * @date 2022/11/9 13:52
     * @author wty
     **/
    public boolean isVarified(String empId, String pwd) {
        Employee employee = employeeService.getEmployeeByEmpidAndPwd(empId, pwd);
        if (null == employee) {
            return false;
        } else {
            System.out.println("---登录成功:" + employee.getName() + "---");
            return true;
        }
    }

    /**
     * 预定座位
     *
     * @param
     * @return void
     * @date 2022/11/9 13:53
     * @author wty
     **/
    public void orderTable() {
        // orderFlag记录是否循环
        boolean orderFlag = true;
        while (orderFlag) {
            System.out.println("请选择要预定餐桌编号(-1退出)");
            Integer id = Utility.readInt(4);
            if (-1 == id) {
                break;
            }
            // 1.检验餐桌编号是否存在 2.该餐桌是否被预定
            // flag:记录餐桌预定情况
            boolean flag = true;
            Object o = diningTableService.queryDiningTableState(id);

            // 检验餐桌编号是否存在
            if (null == o) {
                System.out.println("没有餐桌编号为：" + id + "的餐桌");
                flag = false;
            } else {
                // 2.如果存在，需要查看餐桌的状态
                if ("已预定".equals(o)) {
                    System.out.println("餐桌编号为：" + id + "的餐桌,已经被预定了!");
                    flag = false;
                } else if ("用餐".equals(o)) {
                    System.out.println("餐桌编号为：" + id + "的餐桌,正在用餐!");
                    flag = false;
                }
            }

            if (flag) {
                System.out.println("确认是否预定(Y/N)");
                // 该方法是无限循环:输入的值是Y或者N，如果不是就提示输入错误
                char isOrder = Utility.readConfirmSelection();
//                if ('N' == isOrder) {
//
//                }
                if ('Y' == isOrder) {
                    System.out.println("预定人姓名:");
                    String orderName = Utility.readString(50);
                    System.out.println("预定人电话:");
                    String orderTel = Utility.readString(20);
                    diningTableService.saveOrder(id, orderName, orderTel, "已预定");
                    System.out.println("=====预定成功=====");

                }
            }


        }
    }

    /**
     * 显示菜单
     *
     * @param
     * @return void
     * @date 2022/11/9 13:53
     * @author wty
     **/
    public void showMenu() {
        List<Menu> list = menuService.queryMenu();
        System.out.println("菜品编号\t\t菜品名\t\t\t类别\t\t\t价格 ");
        for (Menu menu : list) {
            System.out.println(menu.getId() + "\t\t\t" + menu.getName()
                    + "\t\t\t" + menu.getType() + "\t\t" + menu.getPrice());
        }
        System.out.println("=====显示完毕=====");
    }

    /**
     * 点餐
     *
     * @param
     * @return void
     * @date 2022/11/9 14:21
     * @author wty
     **/
    public void orderMeals() {
        while (true) {
            System.out.println("请选择点餐的桌号(-1退出)");
            Integer Tableid = Utility.readInt();
            if (-1 == Tableid) {
                break;
            }
            Object o = diningTableService.queryDiningTableState(Tableid);

            if (null == o) {
                System.out.println("输入的餐桌编号:" + Tableid + "不存在");
            } else {

                System.out.println("请选择要菜品编号(-1退出)");
                Integer Menuid = Utility.readInt();
                if (-1 == Menuid) {
                    break;
                }
                Object object = menuService.queryMenuId(Menuid);
                if (null == object) {
                    System.out.println("输入的菜品编号:" + Menuid + "不存在");
                } else {
                    Menuid = (Integer) object;
                    System.out.println("请选择要菜品数量(-1退出)");
                    Integer nums = Utility.readInt();
                    if (-1 == nums) {
                        break;
                    }
                    if (nums <= 0) {
                        System.out.println("输入的菜品数量:" + nums + "不符合规范");
                    } else {
                        System.out.println("确认是否点这个菜(Y/N)");
                        char choose = Utility.readConfirmSelection();

                        if ('Y' == choose) {
                            // 修改餐桌状态
                            diningTableService.updateDiningTableState(Tableid, "用餐");
                            // 生成账单
                            String billId = Utility.getUUID32();

                            // 获取金额
                            Menu menu = menuService.queryMenu(Menuid);
                            Double price = menu.getPrice();

                            int rows = billService.insert(billId, Menuid, nums, price * nums, Tableid, "未结账");
                            System.out.println("点餐结果 = " + rows);

                            System.out.println("=====点餐成功=====");
                        }

                    } // nums else


                } // Menuid else


            } // DiningTableState else

        } // while
    }

    /**
     * 显示账单
     *
     * @param
     * @return void
     * @date 2022/11/9 19:00
     * @author wty
     **/
    public void showBill() {
        List<Bill> list = billService.queryBill();
        System.out.println("编号\t\t菜品号\t\t菜品量\t\t金额\t\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
        for (Bill bill : list) {
            System.out.println(bill.getId() + "\t\t" + bill.getMenuId() + "\t\t\t"
                    + bill.getNums() + "\t\t\t" + bill.getMoney() + "\t\t\t" + bill.getDiningTableId() + "\t\t\t\t\t\t\t"
                    + bill.getBillDate() + "\t\t" + bill.getState());
        }

        System.out.println("=====显示完毕=====");

    }

    /**
     * 结账
     *
     * @param
     * @return void
     * @date 2022/11/9 19:42
     * @author wty
     **/
    public void payAccount() {
        while (true) {
            System.out.println("请选择要结账的餐桌编号(-1退出)");
            Integer diningTableId = Utility.readInt();
            if (-1 == diningTableId) {
                break;
            }
            // 验证餐桌号码是否存在
            Object o = diningTableService.queryDiningTableState(diningTableId);

            if (null == o) {
                System.out.println("输入的餐桌编号:" + diningTableId + "不存在");
            } else {
                String diningTableState = (String) o;

                if ("空".equals(diningTableState)) {
                    System.out.println("餐桌编号为:" + diningTableId + "的餐桌状态为[空闲]，无需支付账单,请核对桌号!");
                }

                if ("已预定".equals(diningTableState)) {
                    System.out.println("餐桌编号为:" + diningTableId + "的餐桌状态为[已预定]，还未点单,无需支付账单,请核对桌号!");
                }

                if ("用餐".equals(diningTableState)) {
                    System.out.println("结账的方式(支付宝/微信/现金)  -- 回车退出");
                    String billState = Utility.readString(3, "9");
                    if ("9".equals(billState)) {
                        break;
                    }

                    System.out.println("确认是否结账(Y/N)");
                    char choose = Utility.readConfirmSelection();

                    if ('Y' == choose) {
                        // 先判断是否有未支付的账单
                        if (billService.checkPayAccount(diningTableId)) {
                            // 修改bill的state
                            int i = billService.updateBillState(diningTableId, billState);

                            // 为保证数据一致性，只有账单更新了，餐桌状态才能更新
                            if (i > 0) {
                                // 修改餐桌表的信息
                                diningTableService.saveOrder(diningTableId, "", "", "空");

                                System.out.println("=====结账完成=====");
                            }

                        } else {
                            System.out.println("检测到没有未支付账单!");
                        }
                    }


                }

            }

        }

    }

    /**
     * 多表联合查询
     *
     * @param
     * @return void
     * @date 2022/11/9 22:49
     * @author wty
     **/
    public void multiQuery() {
        List<MultiTableBean> list = multiTableService.queryMultiTable();
        System.out.println("编号\t\t菜品号\t\t菜品名\t\t菜品价格\t\t菜品量\t\t金额\t\t\t\t桌号\t\t状态");
        for (MultiTableBean multiTableBean : list) {
            System.out.println(multiTableBean.getId() + "\t\t" + multiTableBean.getMenuId() + "\t\t\t"
                    + multiTableBean.getName() + "\t\t" + multiTableBean.getPrice() + "\t\t" + multiTableBean.getNums() + "\t\t\t" + multiTableBean.getMoney() + "\t\t\t" + multiTableBean.getDiningTableId()
                    + "\t\t" + multiTableBean.getState());
        }

        System.out.println("=====显示完毕=====");

    }

    /**
     * 人事管理
     *
     * @param
     * @return void
     * @date 2022/11/10 1:12
     * @author wty
     **/
    public void personMange() {
        while (true) {
            List<Employee> list = employeeService.getEmployeeInfo();
            System.out.println("员工号\t\t员工姓名\t\t职位");
            for (Employee employee : list) {
                System.out.println(employee.getEmpId() + "\t\t" + employee.getName() + "\t\t" + employee.getJob());
            }
            System.out.println("=====人事信息展示完毕=====");
            System.out.println("1.查询具体信息");
//            System.out.println("2.新增雇员");
//            System.out.println("3.删除雇员");
//            System.out.println("9.退出");
            System.out.println("------------------------");
            System.out.println("请输入你的选择:");
            int choose = Utility.readInt();
            if (9 == choose) {
                break;
            } else if (1 == choose) {
                System.out.println("请输入员工号查看具体信息");
                String empId = Utility.readString(50);
                List<EmployeeInfo> infos = employeeInfoService.queryPersonMange(empId);
                System.out.println("员工编号\t\t员工姓名\t\t岗位\t\t性别\t\t出生年月\t\t\t\t\t\t\t" +
                        "身份证号码\t\t\t\t\t\t学历\t\t婚姻状况\t\t电话\t\t\t\t邮箱\t\t\t\t" +
                        "地址\t\t\t\t入职时间\t\t\t\t\t\t\t备注");
                for (EmployeeInfo info : infos) {
                    System.out.println(info.getEmpId() + "\t\t" + info.getName() + "\t\t\t" + info.getJob() + "\t\t"
                            + info.getSex() + "\t\t" + info.getBorndate() + "\t\t\t\t\t\t" + info.getCertno() + "\t\t\t\t"
                            + info.getStudy() + "\t\t" + info.getMarry() + "\t\t" + info.getPhone() + "\t\t" + info.getEmail() + "\t\t"
                            + info.getAddress() + "\t\t" + info.getRegistertime() + "\t\t\t\t" + info.getBz());
                }
                System.out.println("=====查询具体信息(完毕)=====");


            } else {
                System.out.println("输入有误，请重新输入!");
            }
        }

    }

}
